package com.example.schooltest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.schooltest.entity.MyObjectBox;

import com.example.schooltest.entity.UserIm;
import com.example.schooltest.utils.DBOpenHelper;

import io.objectbox.BoxStore;

public class RegistryActivity extends Activity {

    private Bundle bundle;
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private RadioButton radioButton1;
    private EditText edit;
    private EditText edit1;
    private DBOpenHelper dbOpenHelper;
    private BoxStore boxStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        radioGroup = (RadioGroup) findViewById(R.id.rg10);
        radioButton = (RadioButton) findViewById(R.id.rb10);
        radioButton1 = (RadioButton) findViewById(R.id.rb11);
        edit = (EditText) findViewById(R.id.edit10);
        edit1 = (EditText)findViewById(R.id.edit11);
        boxStore = LoginActivity.boxStore;
        UserIm userIm = new UserIm();
        choiceBoyOrGirl(userIm);
        backLogin();
        registry(userIm);

    }

    private void choiceBoyOrGirl(UserIm userIm) {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb10:
                        userIm.setSex(radioButton.getText().toString());
                        Toast.makeText(RegistryActivity.this, "男的", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb11:
                        userIm.setSex(radioButton1.getText().toString());
                        Toast.makeText(RegistryActivity.this, "女的", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    private void backLogin() {
        findViewById(R.id.bul1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RegistryActivity.this, LoginActivity.class));
            }
        });
    }

    private void registry(UserIm userIm) {
        findViewById(R.id.bu10).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userIm.setName(edit.getText().toString());
                userIm.setPassWard(edit1.getText().toString());
                userIm.setNewUser("true");
//                bundle.putString("user", edit.getText().toString());
//                bundle.putString("pass", edit1.getText().toString());
                boxStore.boxFor(UserIm.class).put(userIm);
                startActivity(new Intent(RegistryActivity.this, LoginActivity.class));

            }
        });
    }
}